package com.test.controller;

import com.test.mapper.UserDao;
import com.test.model.BaseModel;
import com.test.model.UserModel;
import com.test.utils.DateUtils;
import com.test.utils.Global;
import com.test.utils.MD5Util;
import com.test.utils.TextUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
@Api(tags = "用户相关接口", description = "UserController系统用户相关接口")
public class UserController extends BaseController {

    @Autowired
    private UserDao userdao;

    @ResponseBody
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    @ApiOperation(value = "新增用户")
    public BaseModel addUser(HttpSession session, UserModel user) {
        if (user == null) {
            return makeModel(ERROR_CODE, "用户不能为空");
        } else {
            if (user.getUsername() == null || user.getPassword() == null) {
                return makeModel(ERROR_CODE, "用户名或者密码不能为空");
            }

            if (userdao.getUserByName(user.getUsername()) == null) {
                user.setUpdateTime(DateUtils.getDate());
                String enPwd = MD5Util.encode(user.getPassword());
                user.setPassword(user.getPassword());
                int code = userdao.add(user);
                if (code == 0) {
                    return makeModel(code, MSG_ADD_ERROR);
                } else {
                    return makeModel(code, MSG_ADD_SUCC);
                }
            } else {
                return makeModel(ERROR_CODE, "用户名已存在");
            }
        }
    }

    @ResponseBody
    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    @ApiOperation(value = "修改用户")
    public BaseModel updateUser(HttpSession session, UserModel user) {
        if (!isLogin(session)) {
            return makeModel(NO_LOGIN, MSG_NO_LOGIN);
        }

        if (user == null) {
            return makeModel(ERROR_CODE, "用户不能为空");
        } else {
            List<UserModel> models = userdao.getUser(user.getId());
            if (models == null || models.size() == 0) {
                return makeModel(ERROR_CODE, "用户不存在");
            } else {
                user.setUpdateTime(DateUtils.getDate());
                if (TextUtils.isEmpty(user.getPassword())) {
                    String enPwd = MD5Util.encode(user.getPassword());
                    user.setPassword(user.getPassword());
                }
                int code = userdao.update(user);
                if (code == 0) {
                    return makeModel(ERROR_CODE, MSG_UPDATE_ERROR);
                } else {
                    return makeModel(SUCC_CODE, MSG_UPDATE_SUCC);
                }
            }

        }
    }

    @ResponseBody
    @GetMapping("/getUser")
    @ApiOperation(value = "获取用户信息", notes = "默认查询所有用户信息，输入userId可查询具体某个用户的信息")
    public BaseModel getUser(HttpSession session, String userId) {
        if (!isLogin(session)) {
            return makeModel(NO_LOGIN, MSG_NO_LOGIN);
        }
        List<UserModel> models = userdao.getUser(userId);
        if (models == null || models.size() == 0) {
            return makeModel(ERROR_CODE, "用户不存在");
        }
        return makeModel(SUCC_CODE, MSG_SUCC, models);
    }

    @ResponseBody
    @RequestMapping("/deleteUser")
    @ApiOperation(value = "删除用户")
    public BaseModel deleteUser(HttpSession session, String userId) {

        if (!isLogin(session)) {
            return makeModel(NO_LOGIN, MSG_NO_LOGIN);
        }

        if (TextUtils.isEmpty(userId)) {
            return makeModel(ERROR_CODE, "用户id 不能为空");
        } else {
            int code = userdao.delete(userId);
            if (code == 0) {
                return makeModel(code, MSG_DELETE_ERROR);
            } else {
                return makeModel(code, MSG_DELETE_SUCC);
            }

        }
    }

    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    @ApiOperation(value = "用户登录", notes = "username,password为必传字段")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "string", name = "username", value = "username", required = true),
            @ApiImplicitParam(paramType = "header", dataType = "string", name = "password", value = "用户登录密码", required = true)})
    public BaseModel login(HttpSession session, String username, String password) {
        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
            return makeModel(ERROR_CODE, "用户名和密码不能为空");
        } else {

            UserModel userModel = userdao.getUserByName(username);
            if (userModel == null) {
                return makeModel(ERROR_CODE, "用户不存在");
            }
            String enPwd = MD5Util.encode(password);
            if (userModel.getPassword().equals(password)) {
                session.setAttribute(Global.USER_SESSION_KEY, userModel);

                return makeModel(SUCC_CODE, MSG_SUCC, userModel);
            } else {
                return makeModel(ERROR_CODE, "用户名或者密码不正确");
            }

        }
    }

    @ResponseBody
    @GetMapping("/loginout")
    @ApiOperation(value = "用户注销")
    public BaseModel loginout(HttpSession session) {
        if (session.getAttribute(Global.USER_SESSION_KEY) == null) {
            return makeModel(NO_LOGIN, MSG_NO_LOGIN);
        } else {
            session.removeAttribute(Global.USER_SESSION_KEY);
            return makeModel(SUCC_CODE, "退出成功");
        }
    }

}
